package com.warrior.configer;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Created by Shawn on 2017/3/17.
 */
public class ConfigHelper {

    private static final Logger logger = LogManager.getLogger(ConfigHelper.class.getName());

    private static Configuration config ;

    private ConfigHelper(){
        //do nothing
    }

    static {
        if (logger.isDebugEnabled()) {
            logger.debug("ConfigHelper() - start"); //$NON-NLS-1$
        }
        FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
                new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
                        .configure(new Parameters().properties()
                                .setFileName("config/config.properties").setEncoding("UTF-8"));
        try {
            config = builder.getConfiguration();
        } catch (ConfigurationException cex) {
            logger.error(cex,cex.fillInStackTrace()); //$NON-NLS-1$
        }

            System.out.println("static语句块执行");

        if (logger.isDebugEnabled()) {
            logger.debug("ConfigHelper() - end"); //$NON-NLS-1$
        }
    }

    public static Configuration getConfig() {
        return config;
    }
}
